#!/bin/bash
source ../config/config.cfg

lpwd=`pwd`

function checkpackagefiles()
{
        echo "检查安装文件..."
        if [ ! -f ${INSTALL_DIR}/data/${FTPSERVER_DIR}/${FTPSERVER_PACKAGE} ]; then
                echo "没有找到安装介质文件 : ${INSTALL_DIR}/data/${FTPSERVER_DIR}/${FTPSERVER_PACKAGE}，请确认..."
                exit 1;
        fi
	sleep 1
}

function check()
{
	checkenvironment ${FTPSERVER_USER} ${FTPSERVER_IP}
	checkworkspace ${FTPSERVER_USER} ${FTPSERVER_IP} ${workspace}
	checkpackagefiles
}

function unzipftpserverpackage()
{
        echo "解压FTPSERVER安装介质..."
	tar zxf ${INSTALL_DIR}/data/${FTPSERVER_DIR}/${FTPSERVER_PACKAGE} -C ${TMP_DIR}
	rm -f ${TMP_DIR}/${FTPSERVER_NAME}/bin/*.bat
	rm -f ${TMP_DIR}/${FTPSERVER_NAME}/bin/*.exe	
	mkdir -p ${TMP_DIR}/${FTPSERVER_NAME}/datas
	sleep 1
}

function modifyconfigfile()
{
        echo "修改配置文件..."
	sed -i "s|$JAVACMD\" -classpath|$JAVACMD\" -Xms${FTPSERVER_JVM}m -Xmx${FTPSERVER_JVM}m -XX:MetaspaceSize=${FTPSERVER_METASPACE}m -XX:MaxMetaspaceSize=${FTPSERVER_METASPACE}m -classpath|g" ${TMP_DIR}/${FTPSERVER_NAME}/bin/ftpd.sh
	sed -i "s|MaxFileSize=10MB|MaxFileSize=${FTPSERVER_MAXFILESIZE}MB|g" ${TMP_DIR}/${FTPSERVER_NAME}/common/classes/log4j.properties
	sed -i "s|MaxBackupIndex=10|MaxBackupIndex=${FTPSERVER_MAXBACKUPINDEX}|g" ${TMP_DIR}/${FTPSERVER_NAME}/common/classes/log4j.properties
	sed -i '/anonymous/d' ${TMP_DIR}/${FTPSERVER_NAME}/res/conf/users.properties
	sed -i "s|21232F297A57A5A743894A0E4A801FC3|`getmd5encrypt ${FTPSERVER_PASSWORD}`|g" ${TMP_DIR}/${FTPSERVER_NAME}/res/conf/users.properties
	sed -i "s|./res/home|${FTPSERVER_DATADIR}|g" ${TMP_DIR}/${FTPSERVER_NAME}/res/conf/users.properties
	sed -i "s/id=\"myServer\"/id=\"myServer\" max-logins=\"999999\" max-threads=\"9999\"/g" ${TMP_DIR}/${FTPSERVER_NAME}/res/conf/ftpd-typical.xml
	sed -i "s/flle-user-manager/file-user-manager encrypt-password=\"clear\"/g" ${TMP_DIR}/${FTPSERVER_NAME}/res/conf/ftpd-typical.xml
	echo "生成启动脚本文件..."
	echo "#!/bin/bash" > ${TMP_DIR}/${FTPSERVER_NAME}/start-ftpserver.sh
	echo "echo \">>>>>> Start FTPSERVER...\"" >> ${TMP_DIR}/${FTPSERVER_NAME}/start-ftpserver.sh
	echo "nohup ${FTPSERVER_HOME}/bin/ftpd.sh res/conf/ftpd-typical.xml 2>&1 >/dev/null &" >> ${TMP_DIR}/${FTPSERVER_NAME}/start-ftpserver.sh
	echo "sleep 1" >> ${TMP_DIR}/${FTPSERVER_NAME}/start-ftpserver.sh
	echo "echo \">>>>>> FTPSERVER Start completed!\"" >> ${TMP_DIR}/${FTPSERVER_NAME}/start-ftpserver.sh
	echo "#!/bin/bash" > ${TMP_DIR}/${FTPSERVER_NAME}/stop-ftpserver.sh
	echo "echo \">>>>>> Stop FTPSERVER...\"" >> ${TMP_DIR}/${FTPSERVER_NAME}/stop-ftpserver.sh
	echo "ps -ef | grep java | grep ${FTPSERVER_HOME} | grep -v grep | awk '{print \$2}' | while read pid" >> ${TMP_DIR}/${FTPSERVER_NAME}/stop-ftpserver.sh
	echo "do" >> ${TMP_DIR}/${FTPSERVER_NAME}/stop-ftpserver.sh
	echo "         kill -9 \${pid}" >> ${TMP_DIR}/${FTPSERVER_NAME}/stop-ftpserver.sh
	echo "done" >> ${TMP_DIR}/${FTPSERVER_NAME}/stop-ftpserver.sh
	echo "sleep 1" >> ${TMP_DIR}/${FTPSERVER_NAME}/stop-ftpserver.sh
	echo "echo \">>>>>> FTPSERVER Stop completed!\"" >> ${TMP_DIR}/${FTPSERVER_NAME}/stop-ftpserver.sh
	chmod +x ${TMP_DIR}/${FTPSERVER_NAME}/start-ftpserver.sh
	chmod +x ${TMP_DIR}/${FTPSERVER_NAME}/stop-ftpserver.sh
	sleep 1
}

function dispatchftpserver()
{
	echo "分发${FTPSERVER_NAME}到服务器${FTPSERVER_IP}"
	cd ${TMP_DIR}
	mv ${FTPSERVER_NAME} ${workspace}
	rm -rf ${TMP_DIR}/${FTPSERVER_NAME}
	cd ${lpwd}
	sleep 1
}

function startftpserver()
{
	echo ">>>>>> Start FTPSERVER..."
	nohup ${FTPSERVER_HOME}/bin/ftpd.sh res/conf/ftpd-typical.xml 2>&1 >/dev/null &
	sleep 1
	echo ">>>>>> FTPSERVER start completed!"
}

function generatescript()
{
        echo "生成启动脚本文件..."
	count=`cat ${workspace}/start.sh | grep start-ftpserver.sh | wc -l`
	if [ 0 == $count ]; then
		echo "${FTPSERVER_HOME}/start-ftpserver.sh" >> ${workspace}/start.sh
	fi
	count=`cat ${workspace}/stop.sh | grep stop-ftpserver.sh | wc -l`
	if [ 0 == ${count} ]; then
		sed -e "1a ${FTPSERVER_HOME}/stop-ftpserver.sh" -i ${workspace}/stop.sh
	fi
	sleep 1
}

starttime=`date +'%Y-%m-%d %H:%M:%S'`
check
unzipftpserverpackage
modifyconfigfile
dispatchftpserver
startftpserver
generatescript

echo "  "
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
echo "本次运行时间： "$((end_seconds-start_seconds))"s"

